<?php
session_start();
/*
 * Session variables:
 * $_SESSION['type'] [string] = user type of the logged in user
 * $_SESSION['authorised'] [boolean] = whether or not the current user is logged in
 */
if(isset($_SESSION['authorised']) && $_SESSION['authorised'] == true)
{   
    if(isset($_SESSION['type']))
    {
        if($_SESSION['type'] == 'administrator')
        {
			include ('connection.inc');
			$action_string = $_POST['action'];
			$action_parts = explode(" ", $action_string);
			$action =  $action_parts[1];
			$teacher_id = $action_parts[0];
			$sql="SELECT mode from teachers where teacher_id=$teacher_id";
			 $result = pg_query($sql);
			 $mode = pg_fetch_result($result, 0, 'mode');
			
			if($action=='Teach')
			{
				$sql="Update teachers set is_available=false where teacher_id=$teacher_id";
				$result=pg_query($sql);
				$sql="Update teachers set teach_time=localtimestamp where teacher_id=$teacher_id";
				$result=pg_query($sql);
			}			
			elseif ($action=='Available')
			{
				$sql="Update teachers set is_available=true where teacher_id=$teacher_id";
				$result=pg_query($sql);
				$sql="Update teachers set num_hours=num_hours+(localtimestamp -teach_time), num_session=num_session+1 where teacher_id=$teacher_id";
				$result=pg_query($sql);
				if($mode=='PER HOUR')
				{
					$sql1 = "Update teachers set salary=( (extract(hour from num_hours)*per_mode) + ((extract(minute from num_hours)/60)*per_mode) ) where teacher_id='$teacher_id'";
					$result1 = pg_query($sql1);
				}
				elseif($mode=='PER SESSION')
				{
					$sql1 = "Update teachers set salary=(num_session*per_mode) where teacher_id='$teacher_id'";
					$result1 = pg_query($sql1);
				}
			}			
			elseif($action=='Out')
			{
				$sql="Update teachers set is_in=false where teacher_id=$teacher_id";
				$result=pg_query($sql);
				$sql="Select is_available from teachers where teacher_id=$teacher_id";
				$result=pg_query($sql);
				if(pg_fetch_result($result, 0, 'is_available')==false)
				{
					$sql="Update teachers set num_hours=num_hours + (teach_time - localtimestamp) where teacher_id=$teacher_id";
					$result=pg_query($sql);
					if($mode=='PER HOUR')
					{
						$sql1 = "Update teachers set salary=( (extract(hour from num_hours)*per_mode) + ((extract(minute from num_hours)/60)*per_mode) ) where teacher_id='$teacher_id'";
						$result1 = pg_query($sql1);
					}
					elseif($mode=='PER SESSION')
					{
						$sql1 = "Update teachers set salary=(num_session*per_mode) where teacher_id='$teacher_id'";
						$result1 = pg_query($sql1);
					}
				}
				$sql="Update teachers set is_available=true where teacher_id=$teacher_id";
				$result=pg_query($sql);
			}			
			
			header('Location: admin_tutorslist.php');
			exit();
        }
        elseif($_SESSION['type'] == 'student')
        {
            header('Location: student_index.php');
            exit();
        }
        elseif($_SESSION['type'] == 'tutor')
        {
            header('Location: tutor_index.php');
            exit();
        }
        elseif($_SESSION['type'] == 'parent')
        {
            header('Location: parent_index.php');
            exit();
        }
    }
}
else
{
    header('Location: index.php');
}
?>
